package com.zeny.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zeny.edu.eduservice.client.VidClient;
import com.zeny.edu.eduservice.entity.EduVideo;
import com.zeny.edu.eduservice.entity.dto.EduVideoDto;
import com.zeny.edu.eduservice.mapper.EduVideoMapper;
import com.zeny.edu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author zeny
 * @since 2020-03-05
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VidClient vidClient;

    /**
     * @Description 根据课程ID删除小节
     * @Date 2020/3/5 0005 13:17
     * @param id
     * @return void
     **/
    @Override
    public void deleteVideoByCourseId(String id) {
        /**
         * @Description 删除多个视频
         * @Date 2020/3/8 0008 8:29
         **/
        QueryWrapper<EduVideo> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.eq("course_id", id);
        wrapperQuery.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapperQuery);
        List<String> list = new ArrayList<>();
        eduVideoList.forEach(eduVideo -> {
            if (StringUtils.isNotBlank(eduVideo.getVideoSourceId())) {
                list.add(eduVideo.getVideoSourceId());
            }
        });
        vidClient.deleteMoreVideo(list);

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        baseMapper.delete(wrapper);
    }

    /**
     * @Description 根据课程ID查询小节
     * @Date 2020/3/5 0005 14:43
     * @param courseId
     * @return java.util.List<com.zeny.edu.eduservice.entity.dto.EduVideoDto>
     **/
    @Override
    public List<EduVideoDto> selectVideoListByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByAsc("sort");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapper);
        List<EduVideoDto> eduVideoDtoList = new ArrayList<>();
        for (EduVideo eduVideo : eduVideoList) {
            EduVideoDto eduVideoDto = new EduVideoDto();
            BeanUtils.copyProperties(eduVideo, eduVideoDto);
            eduVideoDtoList.add(eduVideoDto);
        }
        return eduVideoDtoList;
    }

    /**
     * @Description 判断该章节下面是否存在小节
     * @Date 2020/3/5 0005 19:24
     * @param chapterId
     * @return boolean
     **/
    @Override
    public boolean isChapterExistVideo(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }

    /**
     * @Description 删除小节
     * @Date 2020/3/5 0005 22:47
     * @param videoId
     * @return boolean
     **/
    @Override
    public boolean removeVideo(String videoId) {
        //TODO 删除阿里云视频
        EduVideo eduVideo = baseMapper.selectById(videoId);
        if (StringUtils.isNotBlank(eduVideo.getVideoSourceId())) {
            vidClient.removceVideo(eduVideo.getVideoSourceId());
        }
        int result = baseMapper.deleteById(videoId);
        return result > 0;
    }
}
